﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;

namespace PerceptiveMCAPI.Types
{
	public class chimpChatterOutput : Api_BaseOutput
	{
		public List<chimpChatterResults> result { get; set; }
		public chimpChatterParms inputParms { get; private set; }
		//
		public chimpChatterOutput( chimpChatterInput input )
			: base( input )
		{
			this.inputParms = input.parms;
			this.result = new List<chimpChatterResults>();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return "Not implemented";
		}
		#region ================================================================== format setup
		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}
		// ---- xmlrpc
		internal void Format( x_chimpChatter[] rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}
		#endregion

		#region  ===================================================================== format result
		// ---------------------------------------------------------- outputJSON
		protected void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_chimpChatter[] JSONResult = jss.Deserialize<x_chimpChatter[]>( this.api_Response );
					this.result = FormatResults( JSONResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}

		}
		protected void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			try
			{
				IEnumerable<XElement> message_array = xml.Elements( "struct" );
				x_chimpChatter[] xmlResult = new x_chimpChatter[message_array.Count()];
				int ix = -1;

				foreach ( XElement msg in message_array )
				{
					ix++;
					x_chimpChatter cc = new x_chimpChatter();
					cc.message = msg.Element( "message" ).Value ?? "";
					cc.type = msg.Element( "type" ).Value ?? "";
					cc.campaign_id = msg.Element( "campaign_id" ).Value ?? "";
					cc.list_id = msg.Element( "list_id" ).Value ?? "";
					cc.update_time = msg.Element( "update_time" ).Value ?? "";
					xmlResult[ix] = cc;
				}
				this.result = FormatResults( xmlResult );
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// -------------------------------------------------------------------------- Format results
		private List<chimpChatterResults> FormatResults( x_chimpChatter[] result_array )
		{
			List<chimpChatterResults> results = new List<chimpChatterResults>();
			foreach ( x_chimpChatter item in result_array )
			{
				DateTime dt;
				chimpChatterResults result = new chimpChatterResults();
				result.message = item.message;
				result.type = item.type;
				result.list_id = item.list_id ?? string.Empty;
				result.campaign_id = item.campaign_id ?? string.Empty;
				DateTime.TryParse( item.update_time, out dt );
				result.update_time = dt;
				results.Add( result );
			}
			return results;
		}

		// --------
		#endregion
	}
}
